package com.example.x6.configurationmaintenance.Utils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

public class SuCommand {
    public int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;
        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Throwable th) {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e3) {
                    e3.printStackTrace();
                }
            }
            throw th;
        }
        return result;
    }

    public Vector<String> execRootCmd(String cmd) {
        Vector<String> result = new Vector<>();
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream dos2 = new DataOutputStream(p.getOutputStream());
            DataInputStream dis2 = new DataInputStream(p.getInputStream());
            dos2.writeBytes(cmd + "\n");
            dos2.flush();
            dos2.writeBytes("exit\n");
            dos2.flush();
            while (true) {
                String line = dis2.readLine();
                if (line == null) {
                    break;
                }
                result.add(line);
            }
            p.waitFor();
            if (dos2 != null) {
                try {
                    dos2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dis2 != null) {
                try {
                    dis2.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        } catch (Exception e3) {
            e3.printStackTrace();
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Throwable th) {
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e5) {
                    e5.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e6) {
                    e6.printStackTrace();
                }
            }
            throw th;
        }
        return result;
    }
}
